
/** 失败 */

//成功响应简化
export function sendResponse(res, code, message, data = null) {
    res.status(code).send({
      code,
      message,
      data
    });
  }
//失败响应简化
export function handleError(res, error, defaultMessage = '服务器内部错误') {
    console.error(error);
    const code = error.code === 'ER_DUP_ENTRY' ? 400 : 500;
    const message = error.code === 'ER_DUP_ENTRY' ? '用户名已存在' : defaultMessage;
    sendResponse(res, code, message);
  }

//高阶函数try-catch简化
export function asyncHandler(fn) {
    return async (req, res, next) => {
      try {
        await fn(req, res, next);
      } catch (error) {
        next(error);
      }
    };
  }